package xin.clips.web.controller.gy;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.annotation.Log;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.core.domain.Ztree;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.system.domain.gy.GyKnowBaseType;
import xin.clips.system.service.gy.IGyKnowBaseTypeService;

/**
 * 知识库类型Controller
 *
 * @author yuanzy
 * @date 2023-02-28
 */
@Controller
@RequestMapping("/gy/know_type")
public class GyKnowBaseTypeController extends BaseController {
    private String prefix = "gy/know_type";

    @Autowired
    private IGyKnowBaseTypeService gyKnowBaseTypeService;

    @RequiresPermissions("gy:know_type:view")
    @VisistLog(title = "知识库类型")
    @GetMapping()
    public String know_type() {
        return prefix + "/know_type";
    }

    /**
     * 查询知识库类型列表
     */
    @RequiresPermissions("gy:know_type:list")
    @PostMapping("/list")
    @ResponseBody
    public List<GyKnowBaseType> list(GyKnowBaseType gyKnowBaseType) {
        List<GyKnowBaseType> list = gyKnowBaseTypeService.selectGyKnowBaseTypeList(gyKnowBaseType);
        return list;
    }

    /**
     * 导出知识库类型列表
     */
    @RequiresPermissions("gy:know_type:export")
    @Log(title = "知识库类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyKnowBaseType gyKnowBaseType) {
        List<GyKnowBaseType> list = gyKnowBaseTypeService.selectGyKnowBaseTypeList(gyKnowBaseType);
        ExcelUtil<GyKnowBaseType> util = new ExcelUtil<GyKnowBaseType>(GyKnowBaseType.class);
        return util.exportExcel(list, "知识库类型数据");
    }

    /**
     * 新增知识库类型
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap) {
        mmap.put("knowBaseType", gyKnowBaseTypeService.selectGyKnowBaseTypeById(parentId));
        return prefix + "/add";
    }

    /**
     * 选择知识类型树
     *
     * @param id        知识类型id
     * @param excludeId 排除ID
     */
    @GetMapping(value = {"/selectKnowBaseTypeTree/{id}", "/selectKnowBaseTypeTree/{id}/{excludeId}"})
    public String selectKnowBaseTypeTree(@PathVariable("id") Long id,
                                         @PathVariable(value = "excludeId", required = false) Long excludeId,
                                         ModelMap mmap) {
        mmap.put("knowBaseType", gyKnowBaseTypeService.selectGyKnowBaseTypeById(id));
       // mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /**
     * 新增保存知识库类型
     */
    @RequiresPermissions("gy:know_type:add")
    @Log(title = "知识库类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyKnowBaseType gyKnowBaseType) {
        if (!gyKnowBaseTypeService.checkKnowBaseTypeUnique(gyKnowBaseType)) {
            return error("新增知识库类型'" + gyKnowBaseType.getKnowTypeName() + "'失败，知识库类型已存在");
        }
        return toAjax(gyKnowBaseTypeService.insertGyKnowBaseType(gyKnowBaseType));
    }

    /**
     * 加载知识库列表树
     */
    @RequiresPermissions("gy:know_type:list")
    @GetMapping("/knowTypeTreeData")
    @ResponseBody
    public List<Ztree> knowTypeTreeData() {
        List<Ztree> ztrees = gyKnowBaseTypeService.selectKnowTypeTree(new GyKnowBaseType());
        return ztrees;
    }

    /**
     * 修改知识库类型
     */
    @RequiresPermissions("gy:know_type:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GyKnowBaseType gyKnowBaseType = gyKnowBaseTypeService.selectGyKnowBaseTypeById(id);
        if (ObjectUtil.isNotNull(gyKnowBaseType) && 3l == id) {
            gyKnowBaseType.setParentName("无");
        }
        mmap.put("knowBaseType", gyKnowBaseType);
        return prefix + "/edit";
    }

    /**
     * 修改保存知识库类型
     */
    @RequiresPermissions("gy:know_type:edit")
    @Log(title = "知识库类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyKnowBaseType gyKnowBaseType) {
        if (!gyKnowBaseTypeService.checkKnowBaseTypeUnique(gyKnowBaseType)) {
            return error("新增知识库类型'" + gyKnowBaseType.getKnowTypeName() + "'失败，知识库类型已存在");
        }
        if (gyKnowBaseType.getParentId().equals(gyKnowBaseType.getId())) {
            return error("修改知识库类型'" + gyKnowBaseType.getKnowTypeName() + "'失败，上级知识库类型不能是自己");
        }
        return toAjax(gyKnowBaseTypeService.updateGyKnowBaseType(gyKnowBaseType));
    }

    /**
     * 删除知识库类型
     */
    @RequiresPermissions("gy:know_type:remove")
    @Log(title = "知识库类型", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable(value = "id", required = true) Long id) {
        GyKnowBaseType gyKnowBaseType = new GyKnowBaseType();
        gyKnowBaseType.setParentId(id);
        List<GyKnowBaseType> list  = gyKnowBaseTypeService.selectGyKnowBaseTypeList(gyKnowBaseType);
        if(list!=null && list.size()>0){
            return error("存在下级知识类型，不能删除!");
        }
        return toAjax(gyKnowBaseTypeService.deleteGyKnowBaseTypeById(id));
    }

    /**
     * 加载知识库列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId) {
        GyKnowBaseType knowBaseType = new GyKnowBaseType();
        knowBaseType.setExcludeId(excludeId);
        List<Ztree> ztrees = gyKnowBaseTypeService.selectKnowTypeTreeExcludeChild(knowBaseType);
        return ztrees;
    }

    /**
     * 校验知识库类型
     */
    @PostMapping("/checkKnowBaseTypeUnique")
    @ResponseBody
    public String checkKnowBaseTypeUnique(GyKnowBaseType gyKnowBaseType) {
        boolean flag = gyKnowBaseTypeService.checkKnowBaseTypeUnique(gyKnowBaseType);
        return String.valueOf(flag);
    }

    /**
     * 选择知识库树
     *
     * @param knowType 知识库
     */
    @RequiresPermissions("gy:know_type:list")
    @GetMapping("/selectKnowTypeTree/{knowType}")
    public String selectDeptTree(@PathVariable("knowType") Long knowType, ModelMap mmap) {
        //mmap.put("dept", deptService.selectDeptById(deptId));
        return prefix + "/knwoTypeTree";
    }


}
